package cn.iocoder.an.module.mold.controller.admin.upkeepcat2item;

import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.upkeepcat2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcat2item.UpkeepCat2itemDO;
import cn.iocoder.an.module.mold.service.upkeepcat2item.UpkeepCat2itemService;

@Tag(name = "管理后台 - 模具保养分类项目对照")
@RestController
@RequestMapping("/mold/upkeep-cat2item")
@Validated
public class UpkeepCat2itemController {

    @Resource
    private UpkeepCat2itemService upkeepCat2itemService;

    @PostMapping("/create")
    @Operation(summary = "创建模具保养分类项目对照")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:create')")
    public CommonResult<Long> createUpkeepCat2item(@Valid @RequestBody UpkeepCat2itemSaveReqVO createReqVO) {
        return success(upkeepCat2itemService.createUpkeepCat2item(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具保养分类项目对照")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:update')")
    public CommonResult<Boolean> updateUpkeepCat2item(@Valid @RequestBody UpkeepCat2itemSaveReqVO updateReqVO) {
        upkeepCat2itemService.updateUpkeepCat2item(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具保养分类项目对照")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:delete')")
    public CommonResult<Boolean> deleteUpkeepCat2item(@RequestParam("id") Long id) {
        upkeepCat2itemService.deleteUpkeepCat2item(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具保养分类项目对照")
                @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:delete')")
    public CommonResult<Boolean> deleteUpkeepCat2itemList(@RequestParam("ids") List<Long> ids) {
        upkeepCat2itemService.deleteUpkeepCat2itemListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具保养分类项目对照")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:query')")
    public CommonResult<UpkeepCat2itemRespVO> getUpkeepCat2item(@RequestParam("id") Long id) {
        UpkeepCat2itemDO upkeepCat2item = upkeepCat2itemService.getUpkeepCat2item(id);
        return success(BeanUtils.toBean(upkeepCat2item, UpkeepCat2itemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具保养分类项目对照分页")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:query')")
    public CommonResult<PageResult<UpkeepCat2itemRespVO>> getUpkeepCat2itemPage(@Valid UpkeepCat2itemPageReqVO pageReqVO) {
        PageResult<UpkeepCat2itemDO> pageResult = upkeepCat2itemService.getUpkeepCat2itemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UpkeepCat2itemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出模具保养分类项目对照 Excel")
    @PreAuthorize("@ss.hasPermission('mold:upkeep-cat2item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUpkeepCat2itemExcel(@Valid UpkeepCat2itemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UpkeepCat2itemDO> list = upkeepCat2itemService.getUpkeepCat2itemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具保养分类项目对照.xls", "数据", UpkeepCat2itemRespVO.class,
                        BeanUtils.toBean(list, UpkeepCat2itemRespVO.class));
    }

    @GetMapping("/list-catid")
    @Operation(summary = "获得模具保养分类项目对照")
    @Parameter(name = "catId", description = "类型编号", required = true, example = "1024")
    @TransMethodResult
    public CommonResult<List<UpkeepCat2itemRespVO>> getUpkeepCat2itemByCatId(@RequestParam("catId") Long catId) {
        System.out.println("catId:"+catId);
        List<UpkeepCat2itemDO> list = upkeepCat2itemService.getUpkeepCat2itemByCatId(catId);
        return success(BeanUtils.toBean(list, UpkeepCat2itemRespVO.class));
    }

    @DeleteMapping("/delete-catid")
    @Operation(summary = "删除模具保养分类项目对照")
    @Parameter(name = "catId", description = "编号", required = true)
    public CommonResult<Boolean> deleteUpkeepCat2itemByCatId(@RequestParam("catId") Long catId) {
        upkeepCat2itemService.deleteUpkeepCat2itemByCatId(catId);
        return success(true);
    }

    @PostMapping("/update-catid")
    @Operation(summary = "更新模具保养分类项目对照")
    public CommonResult<Boolean> updateUpkeepCat2itemByCatId(@Valid @RequestBody List<UpkeepCat2itemSaveReqVO> updateReqVO) {
        // 调用服务层方法处理更新逻辑
        upkeepCat2itemService.updateUpkeepCat2itemByCatId(updateReqVO);
        return success(true);
    }

}